package com.ruoyi.web.controller.psi;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.Store;
import com.ruoyi.req.GoodReq;
import com.ruoyi.resp.GoodResp;
import com.ruoyi.resp.StoreResp;
import com.ruoyi.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * lhp
 * 2023/9/7
 * 库存控制层
 */
@RestController
@RequestMapping("/store")
public class StoreController extends BaseController {
    @Autowired
    private StoreService storeService;

    @Log(title = "库存数据", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('psi:store:query')")
    @RequestMapping("/list")
    public TableDataInfo list(Store store){
        startPage();
        List<StoreResp> list = storeService.getList(store);
        return getDataTable(list);
    }

    @Log(title = "入库", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('psi:enterstore:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody List<GoodReq> goods){
        AjaxResult ajaxResult = storeService.add(goods);
        return ajaxResult;
    }
    @Log(title = "导出", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('psi:enterstore:add')")
    @PostMapping("/exportAll")
    public void export(HttpServletResponse response){
        List<StoreResp> rows = storeService.exportAll();
        ExcelUtil<StoreResp> util = new ExcelUtil<StoreResp>(StoreResp.class);
        util.setLock(true);
        util.exportExcel(response, rows, "库存数据");
    }
    @Log(title = "获取总价格", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('psi:store:query')")
    @GetMapping("/getTotal")
    public Double total(){
        Double total = storeService.getTotal();
        return total;
    }
}
